﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using MvvmDatabase.Models;
using SQLite;

namespace MvvmDatabase.Services {
    public class FavoriteStorage : IFavoriteStorage {
        private static readonly string DatabasePath =
            Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder
                    .LocalApplicationData), "MyData.db");

        private SQLiteAsyncConnection _connection;

        private SQLiteAsyncConnection Connection =>
            _connection ??
            (_connection = new SQLiteAsyncConnection(DatabasePath));

        public async Task InitializeAsync() =>
            await Connection.CreateTableAsync<Favorite>();

        public async Task<IList<Favorite>> GetFavoritesAsync() =>
            await Connection.Table<Favorite>().ToListAsync();

        public async Task SaveFavoriteAsync(Favorite favorite) =>
            await Connection.InsertAsync(favorite);
    }
}